package com.qqbot.impl.listener;

import com.qqbot.core.annotation.GroupMessageHandler;
import com.qqbot.core.core.Group;
import com.qqbot.core.core.Member;
import com.qqbot.core.message.MessageChain;
import com.qqbot.impl.service.YouKeApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 截图监听器
 * 
 * @author changwenpeng
 */
@Component
public class ScreenshotListener {

	private static final int NUM_FOUR = 4;

	private Map<Long, Long> tempMap = new HashMap<>();

	private static final long[] screenGroups0 = new long[]{47995581, 565776050};
	private static final long[] screenGroups5 = new long[]{777959736};
	private static final long[] screenGroups10 = new long[]{190124274, 102037756, 189427934, 905244286, 717865685};

	@Autowired
	YouKeApiService youKeApiService;

	/**
	 * 截图1
	 * @param group
	 * @param mc
	 */
	@GroupMessageHandler(groupIds = {478728208}, regex = "^截图电信|截图联通|截图电二|查询电信|查询联通|查询电二$")
	public void screenshot1(Group group, Member member, MessageChain mc) throws Exception{
		int msgid = group.getBot().sendGroupMessage(getScreenGroupId(), new MessageChain().text(sendMsgTxtAndNumber(mc, member)));
		group.getBot().deleteMsg(msgid);
	}

	/**
	 * 截图2
	 * @param group
	 * @param mc
	 */
	@GroupMessageHandler(groupIds = {478728208}, regex = "^(截图电信|截图联通|截图电二|查询电信|查询联通|查询电二)([\\u4e00-\\u9fa5]{1,}|P|D|F|Y|G|H)$")
	public void screenshot2(Group group, Member member, MessageChain mc) throws Exception{
	    String str = "红人";
        if(!mc.getText().endsWith(str)){
			int msgid = group.getBot().sendGroupMessage(getScreenGroupId(), new MessageChain().text(sendMsgTxtAndNumber(mc, member)));
			group.getBot().deleteMsg(msgid);
        }

	}

	/**
	 * 截图3
	 * @param group
	 * @param mc
	 */
	@GroupMessageHandler(groupIds = {478728208}, regex = "^截图商城|截图商城女装|截图商城男装$")
	public void screenshot3(Group group, MessageChain mc) throws Exception{
		int msgid = group.getBot().sendGroupMessage(getScreenGroupId(), mc);
		group.getBot().deleteMsg(msgid);
	}

	/**
	 * 截图4
	 * @param group
	 * @param mc
	 */
	@GroupMessageHandler(groupIds = {478728208}, regex = "^(截图电信|截图联通|截图电二)([^x00-xff]{1,}[^红人])(红人)$")
	public void screenshot4(Group group, MessageChain mc) throws Exception{
		int msgid = group.getBot().sendGroupMessage(getScreenGroupId(), mc);
		group.getBot().deleteMsg(msgid);
	}

	/**
	 * 截图5
	 * @param group
	 * @param mc
	 */
	@GroupMessageHandler(groupIds = {478728208}, regex = "^(截图电信|截图联通|截图电二|查询电信|查询联通|查询电二)([0-9]{1,}|\\(.{1,}\\)|（.{1,}）)$")
	public void screenshot5(Group group, MessageChain mc) throws Exception{
		int msgid = group.getBot().sendGroupMessage(getScreenGroupId(), mc);
		group.getBot().deleteMsg(msgid);
	}


	/**
	 * 截图6
	 * @param group
	 * @param mc
	 */
	@GroupMessageHandler(groupIds = {478728208}, regex = "^(截图电信|截图联通|截图电二|查询电信|查询联通|查询电二)([0-9]{1,}|\\(.{1,}\\)|（.{1,}）)([\\u4e00-\\u9fa5]{1,}|P|D|F|Y|G|H)$")
	public void screenshot6(Group group, MessageChain mc) throws Exception{
		int msgid = group.getBot().sendGroupMessage(getScreenGroupId(), mc);
		group.getBot().deleteMsg(msgid);
	}

	/**
	 * 截图7
	 * @param group
	 * @param mc
	 * @throws Exception
	 */
//	@GroupMessageHandler(groupIds = {478728208L}, regex = "^(截图电信|截图联通|截图电二|查询电信|查询联通|查询电二)(P|D|F|Y|G|H|Q|p|d|f|y|g|h|q)$")
//	public void screenshot7(Group group, MessageChain mc) throws Exception {
//		int msgid = group.getBot().sendGroupMessage(this.getScreenGroupId(), mc);
//		group.getBot().deleteMsg(msgid);
//	}


	/**
	 * 截图8
	 * @param group
	 * @param mc
	 */
	@GroupMessageHandler(groupIds = {478728208}, regex = "^本周活动$")
	public void screenshot8(Group group, MessageChain mc) throws Exception{
		int msgid = group.getBot().sendGroupMessage(189427934, mc);
		group.getBot().deleteMsg(msgid);
	}


	/**
	 * 截图9监听截图群@我的消息
	 * @param group
	 * @param member
	 * @param mc
	 */
	@GroupMessageHandler(groupIds = {47995581, 565776050, 777959736, 190124274, 102037756, 189427934, 905244286, 717865685}, isAt = true)
	public void screenshot9(Group group, Member member, MessageChain mc) throws Exception{
		if(StringUtils.hasText(mc.getImage())){
			group.getBot().sendGroupMessage(478728208, new MessageChain().image(mc.getImage()).text(youKeApiService.randomMsg()));
		}else{
			group.getBot().sendGroupMessage(478728208, new MessageChain().text(mc.getText()));
		}

	}

	/**
	 * 组装截图xx+qq字符串 例如：截图电信2991516121
	 * @param mc
	 * @return
	 */
	private String sendMsgTxtAndNumber(MessageChain mc, Member member) {
		if(mc.hasAt()){
			return mc.getTextAndAt();
		}
		return mc.getText() + member.getUserId();
	}

    /**
     * 组装截图xx 例如：截图商城
     * @param mc
     * @return
     */
    private String sendMsgTxt(MessageChain mc) {
        return mc.getText();
    }

	/**
	 * 获取要截图得群
	 * @return
	 */
	private long getScreenGroupId() throws Exception{
		long groupId = 0;
		while(groupId == 0){
			int i = random(3);
			Long screenGroup0 = i == 0 ? screenGroups0[random(2)] : null;
			Long screenGroup5 = i == 1 ? screenGroups5[0] : null;
			Long screenGroup10 = i == 2 ? screenGroups10[random(5)] : null;
			Long timeMillisNow = System.currentTimeMillis();
			if(null != screenGroup10){
				Long timeMillisMap10 = tempMap.get(screenGroup10);
				if(null != timeMillisMap10){
					long second = (timeMillisNow-timeMillisMap10)/1000;
					if(second > 10){
						groupId = screenGroup10;
						tempMap.put(screenGroup10, timeMillisNow);
					}
				}else{
					groupId = screenGroup10;
					tempMap.put(screenGroup10, timeMillisNow);
				}
			}
			if(null != screenGroup5){
				Long timeMillisMap5 = tempMap.get(screenGroup5);
				if(null != timeMillisMap5){
					long second = (timeMillisNow-timeMillisMap5)/1000;
					if(second > 5){
						groupId = screenGroup5;
						tempMap.put(screenGroup5, timeMillisNow);
					}
				}else{
					groupId = screenGroup5;
					tempMap.put(screenGroup5, timeMillisNow);
				}
			}

			if(null != screenGroup0){
				groupId = screenGroup0;
				tempMap.put(screenGroup0, timeMillisNow);
			}
			Thread.sleep(100);
		}
		return groupId;
	}

	/**
	 * 生成随机数
	 * @return
	 */
	private static int random(int i) {
		return (int) (Math.random() * i);
	}

}
